package accounts

import (
	"gitee.com/slion/account/services"
	"github.com/shopspring/decimal"
	"time"
)

// 账户日志持久化对象(实体类)
type AccountLog struct {
	Id              int64               `db:"id,omitempty"`         //
	LogNo           string              `db:"log_no,uni"`           // 流水编号 全局不重复字符或数字，唯一性标识(唯一索引)
	TradeNo         string              `db:"trade_no"`             // 交易单号 全局不重复字符或数字，唯一性标识
	AccountNo       string              `db:"account_no"`           // 账户编号 账户ID
	UserId          string              `db:"user_id"`              // 用户编号
	Username        string              `db:"username"`             // 用户名称
	TargetAccountNo string              `db:"target_account_no"`    // 目标账户编号 账户ID
	TargetUserId    string              `db:"target_user_id"`       // 目标用户编号
	TargetUsername  string              `db:"target_username"`      // 目标用户名称
	Amount          decimal.Decimal     `db:"amount"`               // 交易金额,该交易涉及的金额
	Balance         decimal.Decimal     `db:"balance"`              // 交易后余额,该交易后的余额
	ChangeType      services.ChangeType `db:"change_type"`          // 流水交易类型，0 创建账户，>0 为收入类型，<0 为支出类型
	ChangeFlag      services.ChangeFlag `db:"change_flag"`          // 交易变化标识：-1出账 1为进账 0创建账户
	Status          int                 `db:"status"`               // 交易状态：-1转账失败 0无效数据 1有效
	Decs            string              `db:"decs"`                 // 交易描述
	CreatedAt       time.Time           `db:"created_at,omitempty"` // 创建时间
}

// AccountTransfer传输层转换为AccountLog持久化对象
func(po *AccountLog) FromTransferDTO(dto services.AccountTransferDTO){

	po.TradeNo = dto.TradeNo
	// 交易主体对象信息
	po.AccountNo = dto.TradeBody.AccountNo
	po.UserId = dto.TradeBody.UserId
	po.Username = dto.TradeBody.Username
	// 交易目标对象信息
	po.TargetAccountNo = dto.TradeTarget.AccountNo
	po.TargetUserId = dto.TradeTarget.UserId
	po.TargetUsername = dto.TradeTarget.Username
	// 其他交易信息
	po.ChangeType = dto.ChangeType
	po.ChangeFlag = dto.ChangeFlag
	po.Amount = dto.Amount
	po.Decs = dto.Desc
}

// AccountLog持久化对象转换为AccountLog传输层对象
func (po *AccountLog) ToDTO() *services.AccountLogDTO{

	AccountLogDTO := &services.AccountLogDTO{}
	AccountLogDTO.TradeNo = po.TradeNo
	AccountLogDTO.LogNo = po.LogNo
	// 交易主体信息
	AccountLogDTO.AccountNo = po.AccountNo
	AccountLogDTO.UserId = po.UserId
	AccountLogDTO.Username = po.Username
	// 交易目标信息
	AccountLogDTO.TargetAccountNo = po.TargetAccountNo
	AccountLogDTO.TargetUserId = po.TargetUserId
	AccountLogDTO.TargetUsername = po.TargetUsername

	AccountLogDTO.Amount = po.Amount
	AccountLogDTO.Balance = po.Balance
	AccountLogDTO.Status = po.Status
	AccountLogDTO.ChangeFlag = po.ChangeFlag
	AccountLogDTO.ChangeType = po.ChangeType
	AccountLogDTO.Decs = po.Decs
	return AccountLogDTO
}